import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import model.User;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

@SuppressFBWarnings("DM_DEFAULT_ENCODING")
public class TcpSocketClient {
    @SuppressFBWarnings("DM_DEFAULT_ENCODING")
    public static void main(String[] args) throws IOException {
        while (true) {
            System.out.println("======您要进行的操作是 1. 录入身份信息 2. 查询身份信息======");
            Scanner scanner = new Scanner(System.in);
            int input = scanner.nextInt();
            switch (input) {
                case 1:
                    regist();
                    break;
                case 2:
                    login();
                    break;
                default:
                    break;
            }
        }
    }

    @SuppressFBWarnings({"DM_DEFAULT_ENCODING", "DM_DEFAULT_ENCODING", "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE"})
    private static void regist() throws IOException {
        // 1. 创建 socket 连接
        Socket socket = new Socket("127.0.0.1",9090);
        // 2. 获取流
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        // 3. 读出用户信息
        String json = getRegistInfo();
        // 4. 发送信息
        bw.write(json);
        bw.newLine();
        bw.flush();

        String[] infos = json.split(",");
        String username = infos[0];
        String password = infos[1];
        // 5. 接收回复
        String reply = br.readLine();
        // 6. 写入日志
        if(username.length() <= 7 && password.length() <= 10) {
            System.out.println("服务器回复："+ reply);
            String content = "{ 用户名: "+ username + "，密码：" + password+" }" + " ---> 录入成功";
            String filepath = ".\\log.txt";
            FileWriter writer;
            try {
                writer = new FileWriter(filepath);
                writer.write("");
                writer.write(content);
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("服务器回复："+ reply);
            String content = "{ 用户名: "+ username + "，密码：" + password+"}" + " ---> 录入失败";
            String filepath = ".\\log.txt";
            FileWriter writer;
            try {
                writer = new FileWriter(filepath);
                writer.write("");
                writer.write(content);
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println("{ 用户名: "+ username + ", 密码：" + password + " }");
        System.out.println();
        // 7. 关闭
        br.close();
        bw.close();
        socket.close();
    }

    @SuppressFBWarnings("DM_DEFAULT_ENCODING")
    private static String getRegistInfo() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您想要录入的用户名：[用户名不能为空,不得超过7字符]");
        String username = scanner.next();
        System.out.println("请输入您想要密码：[密码不能为空,不得超过10字符]");
        String password = scanner.next();
        String json = username + "," + password;
        return json;
    }

    @SuppressFBWarnings({"DM_DEFAULT_ENCODING", "DM_DEFAULT_ENCODING"})
    private static void login() throws IOException {
        // 1. 创建 socket 连接
        Socket socket = new Socket("127.0.0.1",9091);
        // 2. 获取流
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        // 3. 读出用户信息
        String json = getLoginInfo();
        // 4. 发送信息
        bw.write(json);
        bw.newLine();
        bw.flush();
        // 5. 接收回复
        int count = br.read();
        String reply = br.readLine();
        if(count >= 1) {
            System.out.println("服务器回复："+ reply);
        } else {
            System.err.println("服务器回复："+ reply);
        }

        String[] infos = json.split(",");
        String username = infos[0];
        String password = infos[1];
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        System.out.println(user);
        System.out.println();
        // 6. 关闭
        br.close();
        bw.close();
        socket.close();
    }

    @SuppressFBWarnings("DM_DEFAULT_ENCODING")
    private static String getLoginInfo() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您想要查询的用户名：");
        String username = scanner.next();
        System.out.println("请输入密码：");
        String password = scanner.next();
        String json = username + "," + password;
        return json;
    }
}
